package 数据结构练习.summer;


public class day11 {
    //链表准备
    public class ListNode {
        int val;
        ListNode next;
        ListNode() {
        }
        ListNode(int val) {
            this.val = val;
        }
        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
    /*
    给你链表的头结点 head ，请将其按 升序 排列并返回 排序后的链表 。
    https://leetcode.cn/problems/sort-list/
     */
    //时间复杂度为o(n^2),太长的测试用例会超出力扣的时间限制(如1~50000),但短的测试用例可以
  public ListNode sortList(ListNode head) {
        //采用选择排序的思想每次选最小的
        ListNode newHead=new ListNode(0);//新链表的头结点
        ListNode cur=newHead;//新链表的指针
        while(head!=null){
            ListNode temp=head;//记录最小的节点
            ListNode fast=head;//原来链表的指针
            ListNode parent=head;//记录原来节点内最小节点的前一个节点,方便删除原链表最小节点
            while (fast.next!=null){
                //获取最小的节点,因为是.next所以最后一个节点不会判断到
                if (temp.val>fast.next.val){
                    temp=fast.next;
                    parent=fast;
                }
                fast=fast.next;
            }
            //判断最后一个节点是不是最小节点
            if (fast.val< temp.val){
                temp=fast;
                parent=parent.next;
            }
            //删除最小节点
            if (temp==head){
                //刚好是头节点
                head=head.next;
            }else {
                    parent.next=parent.next.next;
            }
            cur.next=temp;
            cur=cur.next;
        }
        return newHead.next;
    }
    //官方解,时间复杂度o(n*logn),自底向上归并排序

    class Solution {
    public ListNode sortList(ListNode head) {
        if (head == null) {
            return head;
        }
        int length = 0;
        ListNode node = head;
        while (node != null) {
            length++;
            node = node.next;
        }
        ListNode dummyHead = new ListNode(0, head);
        for (int subLength = 1; subLength < length; subLength <<= 1) {
            ListNode prev = dummyHead, curr = dummyHead.next;
            while (curr != null) {
                ListNode head1 = curr;
                for (int i = 1; i < subLength && curr.next != null; i++) {
                    curr = curr.next;
                }
                ListNode head2 = curr.next;
                curr.next = null;
                curr = head2;
                for (int i = 1; i < subLength && curr != null && curr.next != null; i++) {
                    curr = curr.next;
                }
                ListNode next = null;
                if (curr != null) {
                    next = curr.next;
                    curr.next = null;
                }
                ListNode merged = merge(head1, head2);
                prev.next = merged;
                while (prev.next != null) {
                    prev = prev.next;
                }
                curr = next;
            }
        }
        return dummyHead.next;
    }

    public ListNode merge(ListNode head1, ListNode head2) {
        ListNode dummyHead = new ListNode(0);
        ListNode temp = dummyHead, temp1 = head1, temp2 = head2;
        while (temp1 != null && temp2 != null) {
            if (temp1.val <= temp2.val) {
                temp.next = temp1;
                temp1 = temp1.next;
            } else {
                temp.next = temp2;
                temp2 = temp2.next;
            }
            temp = temp.next;
        }
        if (temp1 != null) {
            temp.next = temp1;
        } else if (temp2 != null) {
            temp.next = temp2;
        }
        return dummyHead.next;
    }
}



    /*
    给定单链表的头节点head，将所有索引为奇数的节点和索引为偶数的节点分别组合在一起，然后返回重新排序的列表。
    第一个节点的索引被认为是 奇数 ， 第二个节点的索引为偶数 ，以此类推。
    请注意，偶数组和奇数组内部的相对顺序应该与输入时保持一致。
    你必须在O(1)的额外空间复杂度和O(n)的时间复杂度下解决这个问题。
    https://leetcode.cn/problems/odd-even-linked-list
     */
    public ListNode oddEvenList(ListNode head) {
        if (head == null) return head;
        int index = 0;//用于判断奇数偶数
        ListNode cur = head;
        ListNode odd = new ListNode(0), curO = odd;//奇数
        ListNode even = new ListNode(0), curE = even, last = null;//偶数
        while (cur != null) {
            if (index % 2 == 0) {
                //偶数
                curE.next = cur;
                curE = curE.next;
            } else {
                curO.next = cur;
                curO = curO.next;
            }
            cur = cur.next;
            index++;
        }
        curO.next=null;
        curE.next = odd.next;
        return even.next;
    }

}














